// pages/classic/classic.js
import {
  classicModel
} from '../../models/classic.js'
import {
  likeModel
} from '../../models/liek.js'
const classicMode = new classicModel()
const likeMode = new likeModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classic: null,
    frist: true,
    lates: false,
    like: false,
    count: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    classicMode.getlatest((res) => {
      this.setData({
        classic: res.data,
        like: res.data.like_status,
        count: res.data.fav_nums
      })
      classicMode._setLatestindex(this.data.classic.index)
    })
  },

  clikLick: function (even) {
    let likeState = even.detail.likeState
    likeMode.like(likeState, this.data.classic.id, this.data.classic.type)
  },
  // 期刊的更新
  onNext: function (even) {
    this._updateClassic('next')
  },

  onPrevious: function () {
    this._updateClassic('previous')
  },

  _updateClassic: function (statue) {
    let index = this.data.classic.index
    let previous = statue
    classicMode.getClassic(index, (res) => {
      this._getLikestatu(res.data.id, res.data.type)
      this.setData({
        classic: res.data,
        lates: classicMode.isLstest(res.data.index),
        frist: classicMode.isFalst(res.data.index)
      })
    }, previous)

  },

  _getLikestatu: function (artID, category) {
    likeMode.getClassicLikeStatu(artID, category, (res) => {
      this.setData({
        like: res.data.like_status,
        count: res.data.fav_nums
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
